package domain

import (
	"database/sql/driver"
	"errors"
	"fmt"
	jsoniter "github.com/json-iterator/go"
)

type ClubPage struct {
	ID          int64   `json:"id"`
	Title       string  `json:"title" gorm:"type:varchar(255);not null"`
	Content     *JSON   `json:"content" gorm:"type:json"`
	ContentText *string `json:"content_text"`
	Club        *Club   `json:"club" gorm:"foreignKey:PageID"`
}
type ClubPageDTO struct {
	ID          int64    `json:"id"`
	Title       string   `json:"title" gorm:"type:varchar(255);not null"`
	Content     *JSON    `json:"content" gorm:"type:json"`
	ContentText *string  `json:"content_text"`
	Club        *ClubDTO `json:"club" gorm:"foreignKey:PageID"`
}
type JSON map[string]interface{}

func (j JSON) Value() (driver.Value, error) {
	return jsoniter.Marshal(j)
}

func (j *JSON) Scan(input interface{}) error {
	bytes, ok := input.([]byte)
	if !ok {

		return errors.New(fmt.Sprint("Failed to unmarshal JSONB value:", input))
	}
	result := JSON{}
	err := jsoniter.Unmarshal(bytes, &result)
	*j = result
	return err
}
func NewJSON(m map[string]interface{}) JSON {
	j := JSON{}
	for key, value := range m {
		j[key] = value
	}
	return j
}
